package com.fudan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fudan.dao.QuestionDaoImpl;
import com.fudan.dto.entities.Question;
import com.fudan.dto.entities.Room;
import com.fudan.dto.entities.User;


@Transactional
@Service
public class QuestionServiceImpl {
	
	@Autowired
	private RoomServiceImpl roomService;
	
	@Autowired
	private UserServiceImpl userService;
	
	@Autowired
	private QuestionDaoImpl questionDao;
	
	public void like(Integer questionId, Integer userId){
		User user = userService.getUser(userId);
		
		if(user ==null){
			return;
		}
		
		Question question = questionDao.getQuestionById(questionId);
		question.getLikeUser().add(user);
		question = questionDao.save(question);
	}
	
	public Question createQuestion(Integer roomId, String question, Integer userId){
		
		User user =userService.getUser(userId);
		if (user ==null){
			return null;
		}
		
		Room room = roomService.getRoom(roomId);
		
		Question question1 = new Question(question, user);
		room.getQuestions().add(question1);
		roomService.updateRoom(room);
		
		return question1;
	}

}
